<template>
  <div v-if="visible" :style="{ left: left + 'px', top: top + 'px' }" class="context-menu">
    <div class="menu">
      <!-- 刷新 -->
      <div class="menu-item" @click="handleClick('shuaxinshumulu', false)" @mouseover="changeItem('shuaxin')">
        <i class="el-icon-refresh menu-item-icon" style="color: #409EFF;"></i> 刷新
      </div>
      <div class="fgx"></div>
      <!-- 上传文件 -->
      <div :class="(node.pid !== undefined && node.pid !== '0') ? 'menu-item' : 'menu-item-readonly'"
           @click="handleUpload" @mouseover="changeItem('scwj', true)">
        <i class="el-icon-top menu-item-icon" style="color: #0A6ED6;font-weight: 900;"></i>上传文件
        <input ref="upload" type="file" hidden="hidden" @change="handleSuccess">
      </div>

      <!-- 新建文件夹 -->
      <div  :class="(node.pid !== undefined && node.pid !== '0') ? 'menu-item' : 'menu-item-readonly'"
            @click="handleClick('xinjianwenjianjia', true)" @mouseover="changeItem('xjwjj')">
        <i class="iconfont icon-wenjianjia menu-item-icon" style="color: #FDE28F"></i>新建文件夹
      </div>
      <!-- 新建文件 -->
      <div :class="(node.pid !== undefined && node.pid !== '0') ? 'menu-item' : 'menu-item-readonly'"
           @click="handleClick('xinjianwenjian', true)" @mouseover="changeItem('xjwj')">
        <i class="iconfont icon-wendang menu-item-icon" style="color: #909399"></i>新建文件
      </div>
      <div class="fgx"></div>
      <!-- 查看 -->
      <div class="menu-item" @click="handleClick('xiazai', false)" @mouseover="changeItem('chakan')">
        <div ref="focusDiv" tabindex="0" style="width: 100%;float: left">
          <span><i class="el-icon-s-operation menu-item-icon" style="color: #0A6ED6"></i> 查看</span>
          <span style="float:right; margin-right: 5px"><i class="el-icon-arrow-right"></i></span>
        </div>
        <!-- 查看子选项 -->
        <div v-if="option === 'chakan'" @mouseover="changeItem('chakan')" class="context-menu context-option">
          <div style="width: 100%">
            <div class="menu-item" v-for="item in ckList" @click.stop="changeCk(item, 'cklx')">
              <el-row>
                <el-col :span="2">
                  <div style="width: 100%;height:25px"><b v-if="listType === item.type"><i class="el-icon-check" style="color: #409EFF;font-weight: 900"/></b></div>
                </el-col>
                <el-col :span="22"> {{item.name}} </el-col>
              </el-row>
            </div>
          </div>
        </div>
      </div>
      <!-- 排序方式 -->
      <div class="menu-item" @click="handleClick('fuzhi', false)" @mouseover="changeItem('pxfs')">
        <div ref="focusDiv" tabindex="0" style="width: 100%;float: left">
          <span><i class="el-icon-document-copy menu-item-icon" style="color: #0A6ED6"></i> 排序方式</span>
          <span style="float:right; margin-right: 5px"><i class="el-icon-arrow-right"></i></span>
        </div>
        <div v-if="option === 'pxfs'" @mouseover="changeItem('pxfs')" class="context-menu context-option">
          <div style="width: 100%">
            <!-- 查看 -->
            <div class="menu-item" v-for="item in pxList1" @click.stop="changeCk(item, 'pxlx')">
              <el-row>
                <el-col :span="2">
                  <div style="width: 100%;height:25px"><b v-if="pxlx === item.type"><i class="el-icon-check" style="color: #409EFF;font-weight: 900"/></b></div>
                </el-col>
                <el-col :span="22"> {{item.name}} </el-col>
              </el-row>
            </div>
            <div class="fgx"></div>
            <!-- 排序方式 -->
            <div class="menu-item" v-for="item in pxList2" @click.stop="changeCk(item, 'pxlx2')">
              <el-row>
                <el-col :span="2">
                  <div style="width: 100%;height:25px"><b v-if="pxlx2 === item.type"><i class="el-icon-check" style="color: #409EFF;font-weight: 900"/></b></div>
                </el-col>
                <el-col :span="22"> {{item.name}} </el-col>
              </el-row>
            </div>
          </div>
        </div>
      </div>
      <div class="fgx"></div>
      <!-- 属性 -->
      <div class="menu-item" @click="handleClick('shuxing', false)" @mouseover="changeItem('shuxing')">
        <i class="el-icon-warning-outline menu-item-icon" style="color: #6FAFF4"></i>属性
      </div>
    </div>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import axios from 'axios'
export default {
  props: {
    listType: {
      type: String
    }
  },
  data() {
    return {
      src: this.uploadSrc + '/gd/wjglfl/file/upload',
      uploadHeaders: { 'Authorization': 'Bearer ' + getToken(), 'Content-Type': 'multipart/form-data' }, // 如果需要CSRF保护的话
      visible: false,
      left: 0,
      top: 0,
      node: {},
      option: '',
      ckList: [{"name":"图标排列","type":"table"},{"name":"列表排列","type":"list"}],
      pxlx: 'cjsj',
      pxList1: [{"name":"创建时间","type":"cjsj"},{"name":"修改时间","type":"xgsj"},{"name":"文件排序","type":"wjpx"}
        ,{"name":"大小","type":"dx"}],
      pxlx2: 'dz',
      pxList2: [{"name":"递增","type":"dz"},{"name":"递减","type":"dj"}],
    };
  },
  methods: {
    handleUpload() {
      this.$refs.upload.click()
    },
    // 文件上传开始
    handleSuccess(event) {
      const file = event.target.files[0];
      if (file) {
        const formData = new FormData();
        formData.append('file', file);
        formData.append('data', JSON.stringify(this.node));
        axios.post(this.uploadSrc + '/gd/wjglfl/file/upload', formData, { headers: this.uploadHeaders })
          .then(res => {
           this.$emit("refresh")
        })
      } else {
        this.$message.error("请选择文件")
      }
    },
    // 上传失败回调
    handleError(err, file) {
      // 处理错误并通知用户
      this.$message.error('上传失败：', err);
      this.$refs.upload.clearFiles()
    },
    show(event, currentTree) {
      this.visible = true;
      this.left = event.clientX;
      this.top = event.clientY;
      this.node = currentTree
      document.addEventListener('click', this.close);
    },
    close() {
      this.visible = false;
      document.removeEventListener('click', this.close);
    },
    handleClick(action, check) {
      // 如果当前不是只读，则可以进行修改
      if (check) {
        this.$emit('handleClick', action, this.node);
        this.close();
      }else {
        if (this.node.pid !== undefined && this.node.pid !== '0') {
          this.$emit('handleClick', action, this.node);
          this.close();
        }
      }
    },
    changeItem(item) {
      this.option = item
    },
    // 切换查看
    changeCk(item, type) {
      if (type === 'cklx') {
        this.$emit("changeCk", item.type)
      }else {
        this.$set(this, type, item.type)
        let obj = this.pxlx+","+this.pxlx2
        this.$emit("changePx", obj)
      }
    }
  },
};
</script>

<style scoped>
.context-menu {
  position: fixed;
  background: white;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
  z-index: 1000;
  padding: 5px 2px;
  list-style: none;
}
.menu {
  width: 200px;
  height: 100%;
  padding: 0px 5px;
}
.menu-item {
  width: 100%;
  height: 25px;
  padding-left: 4px;
  line-height: 25px;
  font-size: 12px;
  cursor: pointer;
}
.menu-item:hover {
  background-color: #F2F2F2;
  border-radius: 4px;
}
.menu-item-icon {
  margin-right: 2px;
  font-size: 14px;
  height: 25px;
  line-height: 25px;
}
.menu-item-readonly {
  width: 100%;
  height: 25px;
  padding-left: 4px;
  line-height: 25px;
  font-size: 12px;
  opacity: 0.5;
  cursor: not-allowed;
}
.fgx {
  border: 0.1px solid #EBEEF5;
  margin: 4px 0px;
  border-radius: 2px
}
.context-option {
  margin-left: 190px;
  width: 200px;
  padding: 5px;
}
</style>
